package com.shop.dao;

import com.shop.model.Course;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDao {
    public List<Course> searchCourse(int courseId, String courseName){
        List<Course> courses = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from course where courseId = ? or courseName like ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, courseId);
            preparedStatement.setString(2, "%"+courseName+"%");

            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
               Course course = new Course(resultSet.getString("courseName")
                        , resultSet.getString("courseId")
                        , resultSet.getString("Teacher")
                        , resultSet.getDouble("price")
                        , resultSet.getDate("startDate")
                        , resultSet.getDate("endDate")
                        , resultSet.getString("type")
                        , resultSet.getInt("capacity")
                        , resultSet.getInt("time"));
                courses.add(course);
                System.out.println("CourseDao:35:"+course.getCourseName()+" "+course.getCourseId()+" "+course.getTeacher()+" "+course.getPrice()+" "+course.getStartDate()+" "+course.getEndDate()+" "+course.getType()+" "+course.getCapacity()+" "+course.getTime());//调试
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return courses;
    }
    public List<Course> getCourse(){
        List<Course> courses = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from course";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
               Course course =  new Course(resultSet.getString("courseName")
                        , resultSet.getString("courseId")
                        , resultSet.getString("Teacher")
                        , resultSet.getDouble("price")
                        , resultSet.getDate("startDate")
                        , resultSet.getDate("endDate")
                        , resultSet.getString("type")
                        , resultSet.getInt("capacity")
                        , resultSet.getInt("time"));
                courses.add(course);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        for (Course course : courses){
            System.out.println("CourseDao:73:"+course.getCourseName()+" "+course.getCourseId()+" "+course.getTeacher()+" "+course.getPrice()+" "+course.getStartDate()+" "+course.getEndDate()+" "+course.getType()+" "+course.getCapacity()+" "+course.getTime());//调试
        }
        return courses;
    }
    public Course searchCourse1(String courseId){
        Course course = new Course();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from course where courseId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, courseId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                course = new Course(resultSet.getString("courseName")
                        , resultSet.getString("courseId")
                        , resultSet.getString("Teacher")
                        , resultSet.getDouble("price")
                        , resultSet.getDate("startDate")
                        , resultSet.getDate("endDate")
                        , resultSet.getString("type")
                        , resultSet.getInt("capacity")
                        , resultSet.getInt("time"));
                System.out.println("CourseDao:100:"+course.getCourseName()+" "+course.getCourseId()+" "+course.getTeacher()+" "+course.getPrice()+" "+course.getStartDate()+" "+course.getEndDate()+" "+course.getType()+" "+course.getCapacity()+" "+course.getTime());//调试
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        return course;
    }

    public Boolean updateCourseCapacity(List<String> courseIds, int courseCapacity){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            for (String courseId : courseIds){
                String sql = "update course set capacity = capacity-1 where courseId = ?";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, courseId);
                preparedStatement.executeUpdate();

                i++;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
        if(i == courseIds.size()){
            return true;
        }else {
            return false;
        }
    }
}
